package org.apache.struts.register.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class HibernateUtils {

    private static SessionFactory sessionFactory;

    static {
        //获取加载配置管理类
        Configuration configuration = new Configuration();

        //不给参数就默认加载hibernate.cfg.xml文件，
        configuration.configure();

        //创建Session工厂对象
        sessionFactory = configuration.buildSessionFactory();

    }

    /**
     * 获取全局唯一的SessionFactory
     *
     * @return
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * 从全局唯一的SessionFactory中打开一个Session
     *
     * @return
     */
    public static Session openSession() {
        return sessionFactory.openSession();
    }

}
